﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
     [MobileAPI(Code = "MC0400", Name = "用户通过获取未过期的优惠信息")]
    public class UGetPromotionsByShopId : BaseAPI, IProcessMessage
    {
        private BLLUPromotions bllUPromotions;
        public BLLUPromotions BllUPromotions
        {
            get
            {
                if (bllUPromotions == null)
                    bllUPromotions = BLLFactory.getInstance().CreateUPromotionsBLL();
                return bllUPromotions;
            }

        }
        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                string ShopsId = reqMsg.Body.GetFieldValue("ShopsId");
                var flll = reqMsg.Body.GetFieldList("CommodityIds");
                List<String> idList=null;
                if (flll != null)
                {
                    if (flll.Childs.Length != 0)
                    {
                        idList = new List<string>();
                        foreach (var f in flll.GetChilds("CommodityId"))
                        {
                            idList.Add(f.Value);
                        }
                    }
                }
                if (String.IsNullOrEmpty(ShopsId))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }
                List<EPromotions> list = (List<EPromotions>)BllUPromotions.GetPromotionsByShopId(ShopsId,idList);

                if (list != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        var fll = rspMsg.Body.AddFieldList("Promotion");
                        fll.AddField("ShopsId", list[i].ShopsId.TString());
                        fll.AddField("PromotionsId", list[i].PromotionsId.TString());
                        fll.AddField("PromotionsType", list[i].PromotionsType.TString());
                        fll.AddField("PromotionsTitle", list[i].PromotionsTitle.TString());
                        fll.AddField("Discount", list[i].Discount.TString("F1"));
                        fll.AddField("ConditionPrice", list[i].ConditionPrice.TString("F1"));
                        fll.AddField("OffsetPrice", list[i].OffsetPrice.TString("F1"));
                        fll.AddField("CommodityId", list[i].CommodityId.ToString());
                        fll.AddField("StartDate", list[i].StartDate.TString());
                        fll.AddField("EndDate", list[i].EndDate.TString());
                    }

                    rspMsg.Head.RspCode = MessageCode.Success.Code;
                    rspMsg.Head.RspMsg = MessageCode.Success.Msg;
                }
                else
                {
                    rspMsg.Head.RspCode = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;
                }
                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UGetPromotionsByShopId" + e);
                throw e;
            }
        }

        #endregion
    }
}